import uiutil

def PrepareArgs(args):
    dest = {}
    arg = args.split('&')
    for i in arg:
        kv = i.split('=', 1)
        if (len(kv) == 2):
            s = kv[1]
            if dest.has_key(kv[0]):
                if (type(dest[kv[0]]) != type([])):
                    dest[kv[0]] = [dest[kv[0]]]
                dest[kv[0]].append(s)
            else:
                dest[kv[0]] = s
        else:
            dest[i] = ''

    return dest



def Zip(*parts):
    return '\t'.join(parts)



def Unzip(s):
    if ('\t' in s):
        return s.split('\t')
    else:
        return [ each.replace('\\|', '|') for each in s.split('||') ]



def StripTags(s, ignoredTags = None):
    if not ((s.find('<') > -1) and (s.find('>') > -1)):
        return s
    else:
        s = s.strip().split('>')
        for i in xrange(len(s)):
            s[i] = s[i].split('<')
            if ((type(s[i]) != list) or (len(s[i]) == 1)):
                s[i] = [s[i][0], u'']
            html = s[i][1].lower().strip()
            if (ignoredTags is None):
                s[i][1] = u''
            elif len(s[i][1]):
                s[i][1] = ((u'<' + s[i][1]) + u'>')
            handled = 0
            for (tag, attr,) in ignoredTags:
                if ((tag is not None) and html.startswith(tag)):
                    handled = 1
                    if (attr is not None):
                        if (html.find(attr) != -1):
                            s[i][1] = u''
                            break
                    else:
                        break
                else:
                    if ((attr is not None) and (html.find(attr) != -1)):
                        handled = 1
                        s[i][1] = u''
                        break

            if not handled:
                s[i][1] = u''
            s[i] = u''.join(s[i])

        s = u''.join(s)
        return s.strip()



def GetLevel(lvl):
    return ['-',
     'I',
     'II',
     'III',
     'IV',
     'V'][min(5, lvl)]



def IntToRoman(value):
    if ((value > 3999) or (value < 1)):
        return 'N/A'
    else:
        roman = ''
        for (r, v, s,) in (('M',
          1000,
          (('CM', 900),
           ('D', 500),
           ('CD', 400))),
         ('C',
          100,
          (('XC', 90),
           ('L', 50),
           ('XL', 40))),
         ('X',
          10,
          (('IX', 9),
           ('V', 5),
           ('IV', 4))),
         ('I',
          1,
          ())):
            while (value > (v - 1)):
                roman += r
                value -= v

            for (rs, vs,) in s:
                if (value > (vs - 1)):
                    roman += rs
                    value -= vs


        return roman



def RomanToInt(roman):
    d = {'I': 1,
     'V': 5,
     'X': 10,
     'L': 50,
     'C': 100,
     'D': 500,
     'M': 1000}
    nums = []
    for r in roman.upper():
        i = d.get(r, 0)
        if not nums:
            nums.append(i)
            continue
        if (i > nums[-1]):
            nums[-1] = -nums[-1]
        nums.append(i)

    return sum(nums)



def Upper(caption):
    if not caption:
        return ''
    else:
        tbl = '\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde'
        i = 0
        caption = caption.upper()
        for letter in '\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xd7\xf8\xf9\xfa\xfb\xfc\xfd\xfe':
            caption = caption.replace(letter, tbl[i])
            i += 1

        return caption



def UpperCase(string):
    if (session.languageID == 'RU'):
        return string
    else:
        if (string is None):
            return ''
        string = string.upper()
        return string


exports = uiutil.AutoExports('uiutil', locals())

